package org.ihelpuoo.weboo.action.http;

import java.io.IOException;

import org.apache.log4j.Logger;
import org.ihelpuoo.lang.ExceptionUtils;
import org.ihelpuoo.lang.SegmentNotFoundException;
import org.ihelpuoo.weboo.module.ActionContext;
import org.ihelpuoo.weboo.util.XMLPatternSegment;
import org.ihelpuoo.xml.XMLWriter;

public class GetPatternAction implements Action
{
	private static Logger	log	= Logger.getLogger(GetPatternAction.class);

	public void execute(ActionContext context)
	{
		try
		{
			String patternName = context.params().getString("name");
			if (null == patternName)
				throw new SegmentNotFoundException("Pattern name should be be NULL!!!");
			XMLPatternSegment seg = (XMLPatternSegment)context.createPattern(patternName);

			XMLWriter w = XMLWriter.wrap(context.getResponse().getWriter());
			w.setAutoFormat(false);
			w.write(seg.getDocument().getDocumentElement(),0);
			w.flush();
		}
		catch (Exception e)
		{
			try
			{
				context.getResponse().getWriter().write(
						"<b style=color:red>" + e.getMessage() + "</b>");
			}
			catch (IOException e1)
			{
				log.warn(ExceptionUtils.dumpBrief(e1));
			}
		}

	}
}
